package com.cnzz.site1255174697.fragment.main;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.model.TradeResult;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcMyCartsPage;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.utils.SystemBarUtils;
import com.cqyanyu.yanyu.model.EventEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * 购物车
 */
public class CartFragment extends Fragment {
    private View view;
    private WebView webView;
    private boolean isEvent = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_cart, null);
        webView = (WebView) view.findViewById(R.id.mWebView);
        SystemBarUtils.setSystemBarColor(getActivity(), view);
        initView();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                CookieManager cookieManager = CookieManager.getInstance();
                String cookieStr = cookieManager.getCookie(url);
                CookieSyncManager.createInstance(getActivity());
                CookieSyncManager.getInstance().startSync();
                try {
                    int x = cookieStr.indexOf("_w_tb_nick");
                    if (x != -1 && !isEvent) {//一登陆
                        EventEntity eventEntity = new EventEntity();
                        eventEntity.type = 6666;
                        eventEntity.state = cookieStr;
                        EventBus.getDefault().post(eventEntity);
                    }
                    isEvent = false;
                } catch (Exception e) {
                }
            }
        });
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventEntity eventEntity) {
        if (eventEntity.type == 9999) {
            isEvent = true;
            initView();
        } else if (eventEntity.type == 555) {
            initView();
        } else if (eventEntity.type == 222) {
            initView();
        }
    }

    //初始化
    private void initView() {
        Map<String, String> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        //实例化URL打开page
        AlibcBasePage myCartsPage = new AlibcMyCartsPage();
        //设置页面打开方式
        AlibcShowParams showParams = new AlibcShowParams(OpenType.H5, false);
        //使用自己的WebView
        AlibcTrade.show(getActivity(), webView, new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        if (url.startsWith("tel:")) {
                            Intent intent = new Intent(Intent.ACTION_VIEW,
                                    Uri.parse(url));
                            startActivity(intent);
                            return true;
                        } else if (url.startsWith("https://login.m.etao.com")) {
                            EventEntity eventEntity = new EventEntity();
                            eventEntity.type = 6666;
                            EventBus.getDefault().post(eventEntity);
                        }
                        view.loadUrl(url);
                        return true;
                    }
                }, new WebChromeClient(), myCartsPage, showParams, null, exParams,
                new AlibcTradeCallback() {
                    @Override
                    public void onTradeSuccess(TradeResult tradeResult) {
                    }

                    public void onFailure(int code, String msg) {
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
